package springboot.demo.config;


import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocalResovel implements LocaleResolver {

    @Override
    //这个类用来解析请求
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求的语言参数l
      String language=httpServletRequest.getParameter("l");
      //判断这个请求是否为空，如果没有就使用默认的地区，如果有，则分割字符串，更改地区
      Locale local= Locale.getDefault();
      if(!StringUtils.isEmpty(language)){
         String[] split=language.split("_");
         //分割为 国家和地区
         local= new Locale(split[0],split[1]);


      }
        //System.out.println(language);
       // System.out.println(local.toString());

        return local;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
